/*!
\copyright  Copyright (c) 2023 Qualcomm Technologies International, Ltd.
            All Rights Reserved.
            Qualcomm Technologies International, Ltd. Confidential and Proprietary.
\file
\brief      Implementation & Handling of mirror profile background unicast data.
*/

#ifdef ENABLE_LE_MULTIPOINT_BACKGROUND_CIS_SUPPORT

#include "logging.h"
#include "mirror_profile_unicast_background_data.h"
#include "panic.h"
#include <stdlib.h>
#include "gatt.h"

mirror_profile_background_unicast_list_t mirror_unicast_background_list;

mirror_profile_lea_unicast_t* MirrorProfile_GetNextBackgroundUnicastFreeEntry(void)
{
    int index;
    mirror_profile_lea_unicast_t *unicast_free_entry = NULL;

    for (index = 0; index < MIRROR_PROFILE_BACKGROUND_UNICAST_LIST_MAX_SIZE; index++)
    {
        if (BdaddrIsZero(&mirror_unicast_background_list.config[index].bd_addr))
        {
            unicast_free_entry = &mirror_unicast_background_list.config[index];
            break;
        }
    }

    MIRROR_LOG("MirrorProfile_GetNextBackgroundUnicastFreeEntry unicast_free_entry: %p", unicast_free_entry);

    return unicast_free_entry;
}

mirror_profile_lea_unicast_t* MirrorProfile_GetBackgroundUnicastEntryByAddr(const bdaddr *addr)
{
    int index;
    mirror_profile_lea_unicast_t *unicast_entry = NULL;

    for (index = 0; index < MIRROR_PROFILE_BACKGROUND_UNICAST_LIST_MAX_SIZE; index++)
    {
        if (BdaddrIsSame(&mirror_unicast_background_list.config[index].bd_addr, addr) ||
            BdaddrIsSame(&mirror_unicast_background_list.config[index].ia_addr, addr))
        {
            unicast_entry = &mirror_unicast_background_list.config[index];
            break;
        }
    }

    MIRROR_LOG("MirrorProfile_GetBackgroundUnicastEntryByAddr lap 0x%04x, Unicast Entry  0x%x", addr->lap, unicast_entry);

    return unicast_entry;
}

mirror_profile_lea_unicast_t* MirrorProfile_GetBackgroundUnicastEntryByCid(gatt_cid_t cid)
{
    int index;
    mirror_profile_lea_unicast_t *unicast_entry = NULL;

    if (cid == INVALID_CID)
    {
        return NULL;
    }

    for (index = 0; index < MIRROR_PROFILE_BACKGROUND_UNICAST_LIST_MAX_SIZE; index++)
    {
        if (mirror_unicast_background_list.config[index].cid == cid)
        {
            unicast_entry = &mirror_unicast_background_list.config[index];
            break;
        }
    }

    MIRROR_LOG("MirrorProfile_GetBackgroundUnicastEntryByCid cid=0x%x, Unicast Entry 0x%x",cid, unicast_entry);

    return unicast_entry;
}

mirror_profile_lea_unicast_t* MirrorProfile_GetBackgroundUnicastEntryByCisHandle(hci_connection_handle_t cis_handle)
{
    int index;
    mirror_profile_lea_unicast_t *unicast_entry = NULL;

    for (index = 0; index < MIRROR_PROFILE_BACKGROUND_UNICAST_LIST_MAX_SIZE; index++)
    {
        if (cis_handle != MIRROR_PROFILE_CONNECTION_HANDLE_INVALID &&
            (mirror_unicast_background_list.config[index].own_cis_handle == cis_handle ||
             mirror_unicast_background_list.config[index].peer_cis_handle == cis_handle))
        {
            unicast_entry = &mirror_unicast_background_list.config[index];
            break;
        }
    }

    MIRROR_LOG("MirrorProfile_GetBackgroundUnicastEntryByCisHandle CIS Handle = %d, Unicast Entry  0x%x",
                cis_handle,
                unicast_entry);

    return unicast_entry;
}

mirror_profile_lea_unicast_t* MirrorProfile_GetBackgroundUnicastEntryBySource(generic_source_t source)
{
    int index;
    mirror_profile_lea_unicast_t *unicast_entry = NULL;

    for (index = 0; index < MIRROR_PROFILE_BACKGROUND_UNICAST_LIST_MAX_SIZE; index++)
    {
        if (source.type == source_type_voice && 
            mirror_unicast_background_list.config[index].audio_config.voice_source == source.u.voice)
        {
            unicast_entry = &mirror_unicast_background_list.config[index];
            break;
        }

        if (source.type == source_type_audio && 
            mirror_unicast_background_list.config[index].audio_config.audio_source == source.u.audio)
        {
            unicast_entry = &mirror_unicast_background_list.config[index];
            break;
        }
    }

    MIRROR_LOG("MirrorProfile_GetBackgroundUnicastEntryBySource Unicast Entry  Source type %d Unicast 0x%x", source.type, unicast_entry);

    return unicast_entry;
}

void MirrorProfile_ResetUnicastState(mirror_profile_lea_unicast_t *config)
{
    memset(config, 0, sizeof(mirror_profile_lea_unicast_t));

    MIRROR_LOG("MirrorProfile_ResetUnicastState");

    config->cid = INVALID_CID;
    config->own_cis_handle = MIRROR_PROFILE_CONNECTION_HANDLE_INVALID;
    config->peer_cis_handle = MIRROR_PROFILE_CONNECTION_HANDLE_INVALID;
}

void MirrorProfile_InitBackgroundUnicastSourceDataList(void)
{
    int i;

    DEBUG_LOG_FN_ENTRY("MirrorProfile_InitBackgroundUnicastSourceList");

    for (i = 0; i < MIRROR_PROFILE_BACKGROUND_UNICAST_LIST_MAX_SIZE; i++)
    {
        MirrorProfile_ResetUnicastState(&mirror_unicast_background_list.config[i]);
    }
}

#endif /* ENABLE_LE_MULTIPOINT_BACKGROUND_CIS_SUPPORT */

